//
//  DeleteSectionVC.m
//  Demo
//
//  Created by LG on 2018/10/24.
//  Copyright © 2018 LG. All rights reserved.
//

#import "DeleteSectionVC.h"

@interface DeleteSectionVC ()<
UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tview;

@property (nonatomic,strong) NSMutableArray *items;

@end

@implementation DeleteSectionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"积分中心";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.items = [@[@"item1",@"item2",@"item3",@"item4",@"item5",@"item6",@"item7",@"item8",@"item9",@"item10",@"item11",@"item12",@"item13",@"item14",@"item15",@"item16",@"item1",@"item2",@"item3",@"item4",@"item5",@"item6",@"item7",@"item8",@"item9",@"item10",@"item11",@"item12",@"item13",@"item14",@"item15",@"item16"] mutableCopy];
    [self.view addSubview:self.tview];
}

#pragma UITableViewDelegate,UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[self.items objectAtIndex:indexPath.section]];
    return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.items.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"这是文案";
}

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    void (^deleteAction)(NSIndexPath *_indexPath,UITableViewCell *cell) = ^(NSIndexPath *_indexPath,UITableViewCell *cell){
        NSInteger cardIndex = _indexPath.section;
        
        [self.items removeObjectAtIndex:cardIndex];
        NSIndexPath *indexPath = [self.tview indexPathForCell:cell];
        
        if (indexPath != _indexPath) {
            NSLog(@"哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈");
        }
        [self.tview beginUpdates];
        [self.tview deleteSections:[NSIndexSet indexSetWithIndex:cardIndex] withRowAnimation:UITableViewRowAnimationFade];
        [self.tview endUpdates];
    };
//    __weak typeof (self)wSelf= self;
    UITableViewRowAction *acton = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Action title" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        __strong typeof (self)sSelf= wSelf;
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        deleteAction(indexPath,cell);
        
        NSIndexPath *index =[self.tview indexPathForCell:cell];
        NSLog(@"%@",index);
    }];

    return @[acton];
}

#pragma mark -- Getter

- (UITableView *)tview
{
    if (!_tview) {
        _tview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tview.delegate = self;
        _tview.dataSource = self;
    }
    
    return _tview;
}
@end
